<?php
class Resource
{
	protected $database;
	
	protected $resourceID;
	protected $title;
	protected $description;
	protected $keywords;
	
	protected $resourceTypeID;
	protected $statusTypeID;
	
	protected $status;
	protected $leadEditor;
	
	protected $createdBy;
	protected $createdWhen;

	protected $resourceTypes; // 2=RepertoireItem 3=Composer 6=File 7=Link
	protected $users;
	protected $statusTypes;
	
	protected $displayMode = 'view';  // view/edit
	
	protected $currentUserGroup;
	
	
	public function __construct($database,$resourceID)
	{
		$this->database = $database->Recopy();
		$this->resourceID = $resourceID;
		
		if($this->resourceID!=0)
		{
			$sql = "SELECT * from Resources WHERE ResourceID=".$this->resourceID;
//			echo $sql."\n";
			$this->database->ExecuteSQL($sql);
			$row = $this->database->getRow();
			
			$this->resourceID = $row['ResourceID'];
			$this->title = $row['ResourceTitle'];
			$this->description = $row['ResourceDescription'];
			$this->keywords = $row['ResourceKeywords'];
			$this->resourceTypeID = $row['ResourceTypeID'];
			$this->statusTypeID = $row['StatusTypeID'];
			$this->createdBy = $row['CreatedBy'];
			$this->createdWhen = new DateTime($row['CreatedWhen']);
			$this->deletedBy = $row['DeletedBy'];
			$this->deletedWhen = new DateTime($row['DeletedWhen']);
									//			$this->status = $status;
//			$this->leadEditor = $leadEditor;
			
			
		}
		
		/* get arrays */
		$enumResourceTypes = new Enumeration($this->database,'ResourceTypes');
		$this->resourceTypes = $enumResourceTypes->getArray();
				
		$enumUsers = new Enumeration($this->database,'Users');
		$this->users = $enumUsers->getArray();
				
		$enumStatusTypes = new Enumeration($this->database,'StatusTypes');
		$this->statusTypes = $enumStatusTypes->getArray();
				
	}
	
	public function getResourceTypeID()
	{
		return $this->resourceTypeID;
	}
	
	public function setDisplayMode($displayMode)
	{
		$this->displayMode = $displayMode;
	}
	
	public function setCurrentUserGroup($currentUserGroup)
	{
		$this->currentUserGroup = $currentUserGroup;
	}
	
	public function loadData($resourceID, $title, $description, $keywords, $resourceTypeID, $statusType, $leadEditor, $createdBy, $createdWhen, $deletedBy, $deletedWhen)
	{
		$this->resourceID = $resourceID;
		$this->title = $title;
		$this->description = $description;
		$this->keywords = $keywords;
		$this->resourceTypeID = $resourceTypeID;
		$this->statusType = $statusType;
		$this->leadEditor = $leadEditor;
		$this->createdBy = $createdBy;
		$this->createdWhen = $createdWhen;
		$this->deletedBy = $deletedBy;
		$this->deletedWhen = $deletedWhen;
//		$this->status = $status;
	}
	
	public function load($resourceID)
	{
		$this->resourceID = $resourceID;
	}
	
	protected function getViewSummaryHTML()
	{
		$html = "<div class=\"Summary\" id=\"SummaryHTML".$this->resourceID."\" onMouseOver=\"OnMouseOverSummary('SummaryHTML".$this->resourceID."');\" onMouseOut=\"OnMouseOutSummary('SummaryHTML".$this->resourceID."');\" onClick=\"OnClickSummary('".$this->resourceID."');\">\n";
		
//		print_r($this->resourceTypes)."\n";
//		echo "ResourceTypeID = ".$this->resourceTypeID."\nResourceType = ".$this->resourceTypes[$this->resourceTypeID]."<br>\n";
//		echo "ResourceID = ".$this->resourceID."\n<br>\n";

		$overrideDivAction = new Hidden();
		$overrideDivAction->setName("OverrideDivAction");
		$overrideDivAction->setValue("False");
		$html .= $overrideDivAction->getHTML();
			
		if(($this->currentUserGroup=="Editors")||($this->currentUserGroup=="Supervisors"))
		{
			if(($this->resourceTypeID == 2)||($this->resourceTypeID == 3)) //you can only edit RepertoireItem or Composer only
				$html .= "<span class=\"SummaryAdminControls\" id=\"SummaryAdminEdit".$this->resourceID."\" onMouseOver=\"OnMouseOverSummaryAdminEdit('SummaryAdminEdit".$this->resourceID."');\" onMouseOut=\"OnMouseOutSummaryAdminEdit('SummaryAdminEdit".$this->resourceID."');\" onClick=\"OnClickEditResource('".$this->resourceID."');\">Edit</span>\n";
			$html .= "<span class=\"SummaryAdminControls\" id=\"SummaryAdminRemove".$this->resourceID."\" onMouseOver=\"OnMouseOverSummaryAdminRemove('SummaryAdminRemove".$this->resourceID."');\" onMouseOut=\"OnMouseOutSummaryAdminRemove('SummaryAdminRemove".$this->resourceID."');\" onClick=\"OnClickRemoveResource('".$this->resourceID."');\">Remove</span>\n";
		}
		
		$html .= "<span class=\"SummaryTitle\">".$this->title."<br><span style=\"font-size:70%; font-weight:normal;\">(".$this->resourceTypes[$this->resourceTypeID].")</span></span>\n";
		$html .= "<span class=\"SummaryDescription\">".$this->description."</span>\n";

		$html .= "<p class=\"floatstop\" />\n";
		
		$html .= "</div>\n";
		
		return $html;
	}
	
	protected function getEditSummaryHTML()
	{
		$html = "<div class=\"Summary\" id=\"SummaryHTML".$this->resourceID."\" onMouseOver=\"OnMouseOverSummary('SummaryHTML".$this->resourceID."');\" onMouseOut=\"OnMouseOutSummary('SummaryHTML".$this->resourceID."');\" onClick=\"OnClickEditSummary('".$this->resourceID."');\">\n";
		
//		print_r($this->resourceTypes)."\n";
//		print_r($this->users)."\n";
//		echo "ResourceTypeID = ".$this->resourceTypeID."\nResourceType = ".$this->resourceTypes[$this->resourceTypeID]."\n";
		$createdText = "Resource ".$this->resourceID." created by ".$this->users[$this->createdBy]." on ".$this->createdWhen->format("j M Y");
		$statusText = "Status: ".$this->statusTypes[$this->statusTypeID];
		$html .= "<span class=\"SummaryTitle\">".$this->title."<br>\n";
		$html .= "<span style=\"font-size:70%; font-weight:normal;\">";
		$html .= "(".$this->resourceTypes[$this->resourceTypeID].")";
		$html .= "</span></span>\n";
		$html .= "<span class=\"SummaryDescription\">".$this->description."<br><span style=\"font-style: italic; color: #223344; \">&nbsp;";
		$html .= "<br>".$createdText;
		$html .= "<br>".$statusText;
		$html .= "</span></span>\n";

		$html .= "<p class=\"floatstop\" />\n";
		
		$html .= "</div>\n";
		
		return $html;
	}
	
	protected function getSubmitSummaryHTML()
	{
		$html = "<div class=\"Summary\" id=\"SummaryHTML".$this->resourceID."\" onMouseOver=\"OnMouseOverSummary('SummaryHTML".$this->resourceID."');\" onMouseOut=\"OnMouseOutSummary('SummaryHTML".$this->resourceID."');\" onClick=\"OnClickSubmitSummary('".$this->resourceID."');\">\n";
		
//		print_r($this->resourceTypes)."\n";
//		print_r($this->users)."\n";
//		echo "ResourceTypeID = ".$this->resourceTypeID."\nResourceType = ".$this->resourceTypes[$this->resourceTypeID]."\n";
		$createdText = "Resource ".$this->resourceID." created by ".$this->users[$this->createdBy]." on ".$this->createdWhen->format("j M Y");
		$statusText = "Status: ".$this->statusTypes[$this->statusTypeID];
		$html .= "<span class=\"SummaryTitle\">".$this->title."<br>\n";
		$html .= "<span style=\"font-size:70%; font-weight:normal;\">";
		$html .= "(".$this->resourceTypes[$this->resourceTypeID].")";
		$html .= "</span></span>\n";
		$html .= "<span class=\"SummaryDescription\">".$this->description."<br><span style=\"font-style: italic; color: #223344; \">&nbsp;";
		$html .= "<br>".$createdText;
		$html .= "<br>".$statusText;
		$html .= "</span></span>\n";

		$html .= "<p class=\"floatstop\" />\n";
		
		$html .= "</div>\n";
		
		return $html;
	}
	
	protected function getApproveSummaryHTML()
	{
		$html = "<div class=\"Summary\" id=\"SummaryHTML".$this->resourceID."\" onMouseOver=\"OnMouseOverSummary('SummaryHTML".$this->resourceID."');\" onMouseOut=\"OnMouseOutSummary('SummaryHTML".$this->resourceID."');\" onClick=\"OnClickApproveSummary('".$this->resourceID."');\">\n";
		
		$sql = "SELECT DISTINCT id.ActionUserID, n.ShortName FROM Actions as id INNER JOIN Users as n ON id.ActionUserID = n.UserID WHERE id.ResourceID = ".$this->resourceID." AND id.ActionTypeID = 3";
//		echo $sql."<br>\n";
		$this->database->ExecuteSQL($sql);
//		echo $this->Database->getNumRows()."<br>\n";
		$approvers = "";
		$nApprovers = $this->database->getNumRows();
		if($nApprovers>0)
		{
			$approvers .= "( approved by ";
			while($row = $this->database->getRow())
			{
				$approvers .= $row['ShortName']." ";
			}
			$approvers .= ")";
		}
		
		
//		print_r($this->resourceTypes)."\n";
//		print_r($this->users)."\n";
//		echo "ResourceTypeID = ".$this->resourceTypeID."\nResourceType = ".$this->resourceTypes[$this->resourceTypeID]."\n";
		$createdText = "Resource ".$this->resourceID." created by ".$this->users[$this->createdBy]." on ".$this->createdWhen->format("j M Y");
		$statusText = "Status: ".$this->statusTypes[$this->statusTypeID]." ".$approvers;
		$html .= "<span class=\"SummaryTitle\">".$this->title."<br>\n";
		$html .= "<span style=\"font-size:70%; font-weight:normal;\">";
		$html .= "(".$this->resourceTypes[$this->resourceTypeID].")";
		$html .= "</span></span>\n";
		$html .= "<span class=\"SummaryDescription\">".$this->description."<br><span style=\"font-style: italic; color: #223344; \">&nbsp;";
		$html .= "<br>".$createdText;
		$html .= "<br>".$statusText;
		$html .= "</span></span>\n";

		$html .= "<p class=\"floatstop\" />\n";
		
		$html .= "</div>\n";
		
		return $html;
	}
	
	public function getDataHTML()
	{
		$html = "<p class=\"floatstop\" />\n";
		$html .= "<span class=\"ListTitle\">Resource Title:</span><span class=\"ListDescription\">".$this->title."</span>\n";
		$html .= "<p class=\"floatstop\" />\n";
		$html .= "<span class=\"ListTitle\">Resource Description:</span><span class=\"ListDescription\">".$this->description."</span>\n";
		$html .= "<p class=\"floatstop\" />\n";
		$html .= "<span class=\"ListTitle\">Resource Keywords:</span><span class=\"ListDescription\">".$this->keywords."</span>\n";
		$html .= "<p class=\"floatstop\" />\n";
		
		$categories = "";
		$sql = "SELECT cat.*, name.* FROM ChoralCategorys as cat INNER JOIN ChoralCategoryNames as name ON cat.ChoralCategoryNameID = name.ChoralCategoryNameID WHERE cat.ResourceID = ".$this->resourceID;
		$this->database->ExecuteSQL($sql);
		if($this->database->getNumRows()>0)
		{
			while ($row=$this->database->getRow())
			{
				$categories .= "'".$row['Name']."' ";
			}
			$html .= "<span class=\"ListTitle\">Choral Section:</span><span class=\"ListDescription\">".$categories."</span>\n";
			$html .= "<p class=\"floatstop\" />\n";
		}
		
		$categories = "";
		$sql = "SELECT cat.*, name.* FROM OrganCategorys as cat INNER JOIN OrganCategoryNames as name ON cat.OrganCategoryNameID = name.OrganCategoryNameID WHERE cat.ResourceID = ".$this->resourceID;
		$this->database->ExecuteSQL($sql);
		if($this->database->getNumRows()>0)
		{
			while ($row=$this->database->getRow())
			{
				$categories .= "'".$row['Name']."' ";
			}
			$html .= "<span class=\"ListTitle\">Organ Section:</span><span class=\"ListDescription\">".$categories."</span>\n";
			$html .= "<p class=\"floatstop\" />\n";
		}
				
		$categories = "";
		$sql = "SELECT cat.*, name.* FROM LiturgyCategorys as cat INNER JOIN LiturgyCategoryNames as name ON cat.LiturgyCategoryNameID = name.LiturgyCategoryNameID WHERE cat.ResourceID = ".$this->resourceID;
		$this->database->ExecuteSQL($sql);
		if($this->database->getNumRows()>0)
		{
			while ($row=$this->database->getRow())
			{
				$categories .= "'".$row['Name']."' ";
			}
			$html .= "<span class=\"ListTitle\">Liturgy Section:</span><span class=\"ListDescription\">".$categories."</span>\n";
			$html .= "<p class=\"floatstop\" />\n";
		}
				
		$categories = "";
		$sql = "SELECT cat.*, name.* FROM ProfessionalCategorys as cat INNER JOIN ProfessionalCategoryNames as name ON cat.ProfessionalCategoryNameID = name.ProfessionalCategoryNameID WHERE cat.ResourceID = ".$this->resourceID;
		$this->database->ExecuteSQL($sql);
		if($this->database->getNumRows()>0)
		{
			while ($row=$this->database->getRow())
			{
				$categories .= "'".$row['Name']."' ";
			}
			$html .= "<span class=\"ListTitle\">Professional Section:</span><span class=\"ListDescription\">".$categories."</span>\n";
			$html .= "<p class=\"floatstop\" />\n";
		}
		return $html;
	}

	public function getSummaryHTML()
	{
		$html = "";
//		echo $this->displayMode."<br>";
		switch($this->displayMode)
		{
			case 'view':
				$html .= $this->getViewSummaryHTML();
				break;
			
			case 'edit':
				$html .= $this->getEditSummaryHTML();
				break;
			
			case 'submit':
				$html .= $this->getSubmitSummaryHTML();
				break;
			
			case 'approve':
				$html .= $this->getApproveSummaryHTML();
				break;
		}
				
		return $html;

	}
}

?>